/*
 * @(#)PanelTableResult.java Jan 19, 2012 - 10:05:00 PM
 * 
 * Copyright 2011 Cin/UFPE/VSPEED
 */
package br.ufpe.cin.vspeed.gui;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.layout.GroupLayout;

import speed2010.reformulacao.enums.ResultType;
import speed2010.reformulacao.query.InstanceResult;

/**
 * 
 * @author <a href="mailto:andreza.lba@gmail.com">Andreza Leite de Alencar</a>
 */
public class PanelTableResult extends JPanel {
	private static final long serialVersionUID = 7947662157872544281L;

	private JScrollPane jScrollPaneResult;
	private JTable jTableResult;

	private DefaultTableModel tableModel;

	private boolean isResultInformationGenerated;

	/**
	 * Creates new form NewJPanel
	 * 
	 * @param instanceResult
	 */
	public PanelTableResult(final InstanceResult instanceResult) {
		initGUI();
		generateResultInformation(instanceResult);
	}

	private void initGUI() {
		this.jScrollPaneResult = new JScrollPane();
		this.jTableResult = new JTable();

		this.tableModel = updateRows(new ArrayList<String>());
		this.jTableResult.setModel(this.tableModel);

		this.jTableResult.setColumnSelectionAllowed(true);
		this.jTableResult.setSelectionBackground(new java.awt.Color(153, 204, 255));
		this.jTableResult.getTableHeader().setReorderingAllowed(false);

		this.jScrollPaneResult.setViewportView(this.jTableResult);
		this.jTableResult.getColumnModel().getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		final GroupLayout layout = new GroupLayout(this);
		setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap()
						.add(this.jScrollPaneResult, GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE).add(19, 19, 19)));
		layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.LEADING).add(
				layout.createSequentialGroup().addContainerGap()
						.add(this.jScrollPaneResult, GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE).add(19, 19, 19)));
	}

	public DefaultTableModel updateRows(final List<String> arrayList) {
		final String[][] lines = buildInformationArray(arrayList);

		final String[] columns = new String[]{"Result Informations"};
		final DefaultTableModel paramTableModel = new DefaultTableModel(lines, columns) {
			private static final long serialVersionUID = -1507078005332093940L;

			Class<?>[] types = new Class[]{java.lang.String.class};
			boolean[] canEdit = new boolean[]{false};

			@Override
			public Class<?> getColumnClass(final int columnIndex) {
				return this.types[columnIndex];
			}

			@Override
			public boolean isCellEditable(final int rowIndex, final int columnIndex) {
				return this.canEdit[columnIndex];
			}
		};
		this.jTableResult.setModel(paramTableModel);

		this.jTableResult.getTableHeader().setReorderingAllowed(false);
		this.jTableResult.getColumnModel().getSelectionModel()
				.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		this.jTableResult.getColumnModel().getColumn(0).setResizable(true);
		this.jTableResult.getColumnModel().getColumn(0).setHeaderValue("Result Informations");

		this.jTableResult.setColumnSelectionAllowed(false);
		this.jTableResult.setRowSelectionAllowed(false);
		this.jTableResult.setColumnSelectionAllowed(false);
		this.jTableResult.setRowSelectionAllowed(false);
		this.jTableResult.setColumnSelectionAllowed(false);
		this.jTableResult.setRowSelectionAllowed(false);

		return paramTableModel;
	}

	private String[][] buildInformationArray(final List<String> arrayList) {
		String data[][] = new String[][]{};
		if (!arrayList.isEmpty()) {
			final int linesNumber = arrayList.size();
			data = new String[linesNumber][1];

			for (int i = 0; i < arrayList.size(); i++) {
				data[i][0] = arrayList.get(i);
			}
		}

		return data;
	}

	private void generateResultInformation(final InstanceResult instanceResult) {
		if (!this.isResultInformationGenerated && (instanceResult != null)) {
			final List<String> list = new ArrayList<String>();

			String queryEnriched = null;
			final ResultType resultType = instanceResult.getResultType();
			if (resultType == ResultType.EXACT_DESTINY) {
				queryEnriched = "No";
			} else if (resultType == ResultType.ENRICHED) {
				queryEnriched = "Yes";
			}

//			final String query = instanceResult.getOriginalQuery();

//			list.add("<html>Submmited Query: <font color=red>" + query + "</font><html>");
//			list.add("");
			if (resultType == ResultType.ENRICHED) {
				final String enrichedQuery = instanceResult.getEnrichedQuery();
				list.add("<html>Reformulated Query: <font color=red>" + enrichedQuery + "</font><html>");
				list.add("");
			}
			list.add("<html>Enriched Query: <font color=blue>" + queryEnriched + "</font><html>");
			list.add("");
			list.add("<html>" + instanceResult.getInstanceValue() + " is <font color=green>"
					+ instanceResult.getConceptName() + "</font></html>");

			if (resultType == ResultType.ENRICHED) {
				list.add("");
				list.add(instanceResult.getEnrichmentInfo().get(1));
			}

			updateRows(list);

			this.isResultInformationGenerated = true;
		}
	}

}
